package com.pda.gongpin.presenter;


import com.google.gson.Gson;
import com.pda.gongpin.bean.LoginBean;
import com.pda.gongpin.bean.request.LoginRequest;
import com.pda.gongpin.contract.LoginContract;
import com.pda.gongpin.net.NetUtils;
import com.pda.gongpin.utils.UserUtils;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.interceptor.ExCookieInterceptor;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    @Override
    public void signIn(String userName, String password, String ipaddress) {
        LoginRequest loginRequest=new LoginRequest();
        loginRequest.setIpaddress(ipaddress);
        loginRequest.setPassword(password);
        loginRequest.setUserName(userName);
        loginRequest.setLanguage("zh");
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(loginRequest));
        NetUtils.getLoginApi().signIn(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<LoginBean>>() {
                    @Override
                    public void accept(BaseAppEntity<LoginBean> userBeanBaseAppEntity) throws Exception {
                        ExCookieInterceptor.TOKEN=userBeanBaseAppEntity.getData().getToken();
                        getView().onSuccess();
                        getView().showMessage("登录成功！");
                        UserUtils.getInstance().setUserBean(userBeanBaseAppEntity.getData());
                        getView().startIntent(AppActivityKey.HOMEACTIVITY);
                        getView().finishActivity();
                       // getLoginInfo();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getLoginInfo() {
       /* NetUtils.geApi().getLoginInfo()
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserInfo>>() {
                    @Override
                    public void accept(BaseAppEntity<UserInfo> userBeanBaseAppEntity) throws Exception {
                       *//* getView().onSuccess();
                        getView().showMessage("登录成功！");
                        UserUtils.getInstance().setUserBean(userBeanBaseAppEntity.getData());
                        getView().startIntent(AppActivityKey.HOMEACTIVITY);
                        getView().finishActivity();*//*
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                        ExCookieInterceptor.TOKEN="";
                    }
                });*/
    }
}
